<?php

class Sistema_Form_Blog extends Zend_Form {

    public function init() {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;

        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_' . $controller);
        $this->setAttrib('class', 'formCtrl');

        $id = $front->getRequest()->getParam('id', 0);
        
        $el = new Zend_Form_Element_Hidden('BLO_ID', array('disableLoadDefaultDecorators' => true));
        $el->addDecorators(array(array('ViewHelper'), array('Errors')))->addFilter('Int');
        $this->addElement($el);
        
        $query = 'SELECT SQL_CACHE CTB_ID,CTB_NOM FROM CATEGORIA_BLOG WHERE CTB_STA = 1 ORDER BY CTB_NOM ASC';
        $row = $db->fetchAll($query);
        $arr = array();
        foreach($row as $r) $arr[$r['CTB_ID']] = $r['CTB_NOM']; 
        
        $el = new Zend_Form_Element_Select('CTB_ID',array('onchange' => ''));
        $el->setLabel('Categoria')
        ->addMultiOptions($arr);
        $this->addElement($el);

        $el = new Zend_Form_Element_Text('BLO_NOM');
        $el->setLabel('Título')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        /*
        $el = new Zend_Form_Element_Textarea('BLO_DES');
        $el->setLabel('Breve descrição')->setRequired(true)
                ->addFilters(array('StripTags', 'StringTrim'))
                ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        */
        $el = new Zend_Form_Element_Textarea('BLO_TXT');
        $el->setLabel('Texto')->setRequired(true)
            ->addFilters(array('StripTags', 'StringTrim'))
            ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('BLO_DAT');
        $el->setLabel('Data')->setRequired(true);
        $el->setAttrib('required', '');
        $el->setAttrib('class', 'data');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('BLO_TAG');
        $el->setLabel('TAG')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        
        $img = glob('public/upload/'.$controller.'/'.$id.'.*');
        if(is_array($img))
        foreach($img as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('BLO_FILE');
        $el->setLabel('Imagem Título - Tamanho: 700x325')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $this->addElement($el);
        
        /*
        $img = glob('public/upload/'.$controller.'/'.$id.'_txt.*');
        if(is_array($img))
        foreach($img as $src) {
            $el = new Zend_Form_Element('img2',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('BLO_FILE2');
        $el->setLabel('Imagem Texto - Tamanho 606x210')
	->addValidator('Extension', false, array('jpg','jpeg','png', 'gif'));
        $this->addElement($el);
        */
        
        $el = new Zend_Form_Element_Checkbox('BLO_STA',array('checked'=>'checked'));
        $el->setLabel('Ativo')->setRequired(true);
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('Salvar', array('disableLoadDefaultDecorators' => true));
        $btn->setLabel('Salvar');
        $btn->addDecorators(array(array('ViewHelper'), array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);

        $btn = new Zend_Form_Element_Button('Cancelar', array('disableLoadDefaultDecorators' => true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="' . $url . '" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
    }

}
